package test.test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.IOException;
import java.io.StringReader;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

/**
 * Created by zt on 2016/6/27.
 */
public class Utils {

	// setRequestProperty设置通用请求属性
	// User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
	// (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
	private static void setRequestPro(HttpURLConnection urlConn) {

		urlConn.setRequestProperty("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
		urlConn.setRequestProperty("Accept",
				"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
		urlConn.setRequestProperty("Accept-Language", "zh-cn");
		urlConn.setRequestProperty("Accept-Encoding", "gzip");
		urlConn.setRequestProperty("Content-type", "text/html");
		urlConn.setRequestProperty("Connection", "close"); // keep-Alive，

		urlConn.setUseCaches(false);
		urlConn.setConnectTimeout(10 * 1000);
		urlConn.setReadTimeout(10 * 1000);
		urlConn.setDoOutput(true);
		urlConn.setDoInput(true);
	}

	public static boolean isNotEmpty(Object o) {
		if (null == o) {
			return false;
		}
		if (o.toString().equals("")) {
			return false;
		}
		return true;
	}

	// 输出结果写入文件
	public static void write2File(InputStream in, String path, String fileName, String charset) {
		File fp = new File(path);
		if (!fp.exists()) {
			fp.mkdirs();
		}
		try {

			String filePath = fp + "\\" + fileName;
			PrintWriter pw = null;
			FileOutputStream fos = new FileOutputStream(filePath);
			OutputStreamWriter writer = new OutputStreamWriter(fos, "utf-8");
			pw = new PrintWriter(writer);
			BufferedReader bReader = new BufferedReader(new InputStreamReader(in, charset));
			StringBuffer sb = new StringBuffer();
			String rLine = null;
			while ((rLine = bReader.readLine()) != null) {
				pw.println(rLine);
				pw.flush();
			}
			in.close();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 结果写成字符串
	public static void write2String(InputStream input, String charset) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				sb.append(line).append("\r\n");
			}
			if (reader != null) {
				reader.close();
			}
			System.out.println(sb.toString());///////////////////// 打印网页内容
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 得到指定网页的html文件
	public static String GetWebContent(String urlString, final String charset, int timeout, String outpath)
			throws IOException {
		if (urlString == null || urlString.length() == 0) {
			return null;
		}
		urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString
				: ("http://" + urlString);
		URL url = new URL(urlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestProperty("Pragma", "no-cache");
		conn.setRequestProperty("Cache-Control", "no-cache");

		int temp = Integer.parseInt(Math.round(Math.random() * 7) + "");
		String ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
		conn.setRequestProperty("User-Agent", ua);// UserAgent[temp]);
		conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// 只接受text/html类型，当然也可以接受图片,pdf,*/*任意，就是tomcat/conf/web里面定义那些
		conn.setConnectTimeout(timeout);
		try {
			System.out.println("conn.getResponseCode():" + conn.getResponseCode());
			System.out.println("conn.getResponseMessage():" + conn.getResponseMessage());

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				return null;
			}
		} catch (Exception e) {
			try {
				e.printStackTrace();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			return null;
		}

		InputStream input = conn.getInputStream();
		// write2String(input,charset);
		write2File(input, outpath, "new.163.html", charset);
		if (conn != null) {
			conn.disconnect();
		}
		return null;
	}

	// 得到html文件的中文内容
	public static void GetHtmlContent(String inputurl, File outFile) throws IOException {
		//String temppath = "g:\\temp\\download";
		String temppath = "download";
		GetWebContent(inputurl, "gbk", 10000, temppath);
		File file = new File(temppath + "\\" + "new.163.html");
		if (file.exists() && file.isFile()) {
			FileInputStream fileInputs = null;
			// main
			try {

				fileInputs = new FileInputStream(file);

				InputStreamReader inputStreamReader = new InputStreamReader(fileInputs);

				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

				String line = null;

				FileOutputStream fileOutputStream = null;

				fileOutputStream = new FileOutputStream(outFile);

				if (!outFile.exists())

					outFile.createNewFile();

				BufferedOutputStream bufferedOutputStream = null;

				bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
				
				//中文分词器和汉字正则表达式
				
				StringReader sr = null;

				IKSegmenter ik = null;

				Lexeme lex = null;

				while ((line = bufferedReader.readLine()) != null) {
					if (getChinese(line) != null) {

						sr = new StringReader(getChinese(line));

						ik = new IKSegmenter(sr, true);

						while ((lex = ik.next()) != null) {

							System.out.print(lex.getLexemeText() + "\n");

							fileOutputStream.write(lex.getLexemeText().getBytes());

							fileOutputStream.write("\n".getBytes());

						}
					}
				}

				fileOutputStream.flush();

				fileOutputStream.close();

			} catch (FileNotFoundException e) {

				e.printStackTrace();

			} catch (IOException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		} else {

			System.out.println("not a file or not exits!!");

			System.exit(0);

		}

	}

	// 利用正则表达式得到中文内容
	public static String getChinese(String content) {
		String regex1 = "[\u4e00-\u9fa5]+";// 中文

		Pattern p = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);

		Matcher m = p.matcher(content);

		boolean blnp = m.find();

		if (blnp == true) {

			return m.group();

		}

		return null;

	}


}
